package com.ts.code.creator.creator.impl;

import com.ts.code.creator.enums.CreateFileEnum;
import com.ts.code.creator.enums.ParamTypeEnum;
import com.ts.code.creator.enums.RetTypeEnum;
import com.ts.code.creator.util.CreateCodeContext;
import com.ts.code.creator.util.StringUtil;

/**
 * 聚合层 controller
 */
public class AdminServiceImplCreator extends AbstractCodeCreator {

    public AdminServiceImplCreator(CreateCodeContext context) {
        super(context);
    }

    @Override
    protected CreateFileEnum createFileEnum() {
        return CreateFileEnum.ADMIN_SERVICE_IMPL;
    }

    @Override
    public void doCreate() {
        // 聚合层 serviceImpl
        addMethod(classFileSearch.adminServiceImpl(), adminServiceImpl(context));
    }

    private String adminServiceImpl(CreateCodeContext context) {
//        String text = "@Override\n" +
//                "    public %s %s(%s) {\n" +
//                "        IdsReqDTO reqDTO = map(param, IdsReqDTO.class);\n" +
//                "        return map(Client.execute(() -> welfareClient.queryStatus(reqDTO))\n" +
//                "                .ifThrow(WelfareErrorEnum.class)\n" +
//                "                .orElseGet(WelfareQueryStatusRespDTO::new), WelfareQueryStatusVO.class);\n" +
//                "    }";

        // 返回类型
        String returnType = adminVO();
        // 参数
        String paramStr = adminParam();

        String text = "@Override\n";
        text += String.format("public %s %s(%s) {\n", returnType, context.getMethodName(), paramStr);

        // 参数转换行
        switch (context.getParamTypeEnum()) {
            case NONE:
                text += "";
                break;
            case ID:
                text += "IdReqDTO reqDTO = map(param, IdReqDTO.class);\n";
                break;
            case PAGE:
            case COMMON:
                String reqDTOClass = reqDTOClass();
                text += String.format("%s reqDTO = map(param, %s.class);\n", reqDTOClass(), reqDTOClass);
        }

        String exec = String.format("%sClient.%s(%s)", StringUtil.firstLower(context.getEntityName()), context.getMethodName(),
                context.getParamTypeEnum() == ParamTypeEnum.NONE ? "" : "reqDTO");

        String body = "";

        String execute = "";
        if (context.getRetTypeEnum() == RetTypeEnum.PAGE) {
            execute = "PageClient.execute(() -> " + exec + ")\n";
            execute += String.format(".map(%s.class)\n", className("VO"));
            body = "return " + execute + ";";

        } else {
            execute = "Client.execute(() -> " + exec + ")\n";
            execute += String.format(".ifThrow(%sErrorEnum.class)\n", context.getEntityName());

            switch (context.getRetTypeEnum()) {
                case NULL:
                    execute += ".orElseGet(NullRespDTO::new)";
                    break;
                case ID:
                    execute += ".orElseGet(IdRespDTO::new)";
                    break;
                case LIST:
                    execute += ".orElseGet(ArrayList::new)";
                    break;
                case PAGE:
                    break;
                case COMMON:
                    execute += String.format(".orElseGet(%s::new)", respDTOClass());
            }

            if (context.getRetTypeEnum() == RetTypeEnum.LIST) {
                body = String.format("return mapList(%s, %s.class, %s.class);\n", execute, respDTOClass(), adminVOClass());
            } else {
                body = String.format("return map(%s, %s.class);\n", execute, returnType);
            }
        }

        return text + body + "}";
    }

}
